/*
 * Copyright (C), 2014-2015, 杭州小卡科技有限公司
 * Created by ciwei@xiaokakeji.com on 2016/06/21.
 */
package com.micromall.admin;

import com.micromall.repository.AdminUserMapper;
import com.micromall.repository.entity.AdminUser;
import com.micromall.service.TransactionalExecuteService;
import com.micromall.utils.ArgumentValidException;
import com.micromall.utils.ValidateUtils;
import com.micromall.web.resp.ResponseEntity;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author ciwei@xiaokakeji.com
 * @date 2016/06/21.
 */
@RestController
@RequestMapping(value = "/admin/user")
public class AdminUserController extends AdminBasicController {

	@Resource
	private AdminUserMapper             adminUserMapper;
	@Resource
	private TransactionalExecuteService transactionalExecuteService;

	@RequestMapping(value = "/list")
	@Transactional(readOnly = true)
	public ResponseEntity<?> list() {
		return ResponseEntity.Success(adminUserMapper.selectList());
	}

	@RequestMapping(value = "/get")
	@Transactional(readOnly = true)
	public ResponseEntity<?> get(int id) {
		return ResponseEntity.Success(adminUserMapper.get(id));
	}

	private AdminUser validateParams(String username, String name) {
		if (StringUtils.isBlank(username)) {
			throw new ArgumentValidException("登录用户名不能为空");
		}
		if (StringUtils.isBlank(name)) {
			throw new ArgumentValidException("账号使用人不能为空");
		}
		if (ValidateUtils.illegalTextLength(3, 15, username)) {
			throw new ArgumentValidException("登录用户名长度只能4-10位");
		}
		if (ValidateUtils.illegalTextLength(1, 15, name)) {
			throw new ArgumentValidException("账号使用人名称长度超过限制");
		}
		AdminUser adminUser = new AdminUser();
		adminUser.setUsername(username);
		adminUser.setName(name);
		return adminUser;
	}

	/**
	 * @return
	 */
	@RequestMapping(value = "/add")
	@Transactional
	public ResponseEntity<?> add(String username, String password, String name, boolean enable) {
		AdminUser adminUser = validateParams(username, name);
		if (adminUserMapper.existsUsername(username) > 0) {
			throw new ArgumentValidException("用户名已经存在");
		}
		if (StringUtils.isBlank(password)) {
			throw new ArgumentValidException("登录密码不能为空");
		}
		if (ValidateUtils.illegalTextLength(5, 15, password)) {
			throw new ArgumentValidException("登录密码长度只能5-15位");
		}
		adminUser.setEnable(enable);
		adminUser.setInternal(false);
		adminUser.setPassword(DigestUtils.md5Hex("$" + password + "$"));
		adminUser.setCreateTime(new Date());
		transactionalExecuteService.execute(() -> adminUserMapper.insert(adminUser));
		return ResponseEntity.Success();
	}

	@RequestMapping(value = "/update")
	@Transactional
	public ResponseEntity<?> update(int id, String username, String password, String name, boolean enable) {
		AdminUser user = adminUserMapper.get(id);
		if (user == null) {
			throw new ArgumentValidException("用户不存在");
		}
		/*AdminUser adminUser = validateParams(username, name);
		if (adminUserMapper.existsUsernameInUpdate(id, username) > 0) {
			throw new ArgumentValidException("用户名已经存在");
		}
		if (StringUtils.isNotBlank(password)) {
			if (ValidateUtils.illegalTextLength(5, 15, password)) {
				throw new ArgumentValidException("登录密码长度只能5-15位");
			}
			adminUser.setPassword(DigestUtils.md5Hex("$" + password + "$"));
		} else {
			adminUser.setPassword(user.getPassword());
		}
		adminUser.setId(id);

		adminUser.setUpdateTime(new Date());*/
		AdminUser adminUser = new AdminUser();
		if (StringUtils.isBlank(password)) {
			throw new ArgumentValidException("登录密码不能为空");
		}
		if (ValidateUtils.illegalTextLength(5, 15, password)) {
			throw new ArgumentValidException("登录密码长度只能5-15位");
		}
		adminUser.setId(id);
		adminUser.setEnable(enable);
		adminUser.setPassword(DigestUtils.md5Hex("$" + password + "$"));
		adminUser.setUpdateTime(new Date());
		transactionalExecuteService.execute(() -> adminUserMapper.updatePassword(adminUser));
		return ResponseEntity.Success();
	}

	@RequestMapping(value = "/delete")
	@Transactional
	public ResponseEntity<?> delete(String ids) {
		if (StringUtils.isBlank(ids)) {
			throw new ArgumentValidException("参数错误");
		}
		transactionalExecuteService.execute(() -> {
			for (String id : ids.split(",")) {
				adminUserMapper.delete(Integer.valueOf(id));
			}
		});
		return ResponseEntity.Success();
	}
}
